package com.wh.org.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;

import com.wh.basic.vo.AjaxResult;
import com.wh.org.dto.DepartmentDto;
import com.wh.org.pojo.Department;
import com.wh.org.service.IDepartmentService;

import com.wh.system.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;

    @PreAuthorize(sn = "department/addOrUpd",name = "部门添加或修改")
    @PutMapping
    public AjaxResult addOrUpd(@RequestBody Department department) {
        try {
            if (department.getId() != null) { //说明是修改
                service.upd(department);
            } else {
                service.add(department);
            }
            return AjaxResult.me();
        } catch (Exception ex) {
            return AjaxResult.me().setSuccess(false).setMessage("出现异常");
        }
    }

    @PreAuthorize(sn = "department/delById",name = "根据id删除部门")
    @DeleteMapping("/{id}")
    public AjaxResult delById(@PathVariable("id") Long id) {
            service.del(id);
            return AjaxResult.me();

    }

    @PreAuthorize(sn = "department/findAll",name = "查询所有部门")
    @GetMapping
    public List<Department> findAll() {
        return service.findAll();
    }

    @PreAuthorize(sn = "department/findById",name = "根据id查部门")
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id) {
        return service.findById(id);
    }

    @PreAuthorize(sn = "department/findByPage",name = "分页查找部门")
    @PostMapping
    public IPage<Department> findByPage(@RequestBody DepartmentDto departmentDto) {
        return service.findByPage(departmentDto);
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            service.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception ex) {
            return AjaxResult.me().setSuccess(false).setMessage("出现异常");
        }
    }
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
      return service.deptTree();
    }


}
